package com.hz.ruoyiequipment.data.controller;

import com.hz.ruoyiequipment.data.domain.AgrEquipment;
import com.hz.ruoyiequipment.data.domain.AgrEquipmentTr;
import com.hz.ruoyiequipment.data.domain.vo.SoilEquipment;
import com.hz.ruoyiequipment.data.domain.vo.SoilTimeData;
import com.hz.ruoyiequipment.data.service.AgrEquipmentService;
import com.hz.ruoyiequipment.data.service.AgrEquipmentTrService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: xqd
 * @CreateDate: 2025/03/08/9:26
 * @Description: 土壤墒情设备控制层
 */
@RestController
@RequestMapping("/soil")
@CrossOrigin
public class AgrEquipmentTrController extends BaseController {
    @Autowired
    private AgrEquipmentService agrEquipmentService;
    @Autowired
    private AgrEquipmentTrService agrEquipmentTrService;
    @RequiresPermissions("data:soil:timedata")
    @GetMapping("/timedata")
    public AjaxResult getSoilTimeData(Long equipmentId){
        return agrEquipmentTrService.getSoilTimeData(equipmentId);
    }

    @RequiresPermissions("data:soil:timedatas")
    @GetMapping("/timedatas")
    public AjaxResult getSoilTimeDatas(Long equipmentId){
        return agrEquipmentTrService.getSoilTimeDatas(equipmentId);
    }


    @RequiresPermissions("data:soil:head")
    @GetMapping("/head")
    public AjaxResult getSoilTimeDataHead(Long equipmentId){
        return agrEquipmentTrService.getSoilTimeDataHead(equipmentId);
    }


    @RequiresPermissions("data:soil:list")
    @GetMapping("/list")
    public TableDataInfo getSoilEquipment(Integer equipmentStatus, String equipmentCode, String plotsName){
        startPage();
        return getDataTable(agrEquipmentService.getSoilEquipment(equipmentStatus,equipmentCode,plotsName));
    }
    @RequiresPermissions("data:soil:detail")
    @GetMapping("/detail")
    public AjaxResult getSoilDetail(Long equipmentId){
        return agrEquipmentService.getSoilDetail(equipmentId);
    }
    @RequiresPermissions("data:soil:add")
    @PostMapping("/insert")
    public AjaxResult insertSoil(@RequestBody SoilEquipment soilDetail){
        try {
            AgrEquipment agrEquipment = new AgrEquipment();
            agrEquipment.setEquipmentCode(soilDetail.getEquipmentCode());
            agrEquipment.setEquipmentName(soilDetail.getEquipmentName());
            agrEquipment.setEquipmentBrand(soilDetail.getEquipmentBrand());
            agrEquipment.setEquipmentType(soilDetail.getEquipmentType());
            agrEquipment.setEquipmentImg(soilDetail.getEquipmentImg());

            AgrEquipmentTr agrEquipmentTr = getAgrEquipmentTr(soilDetail);

            agrEquipmentService.insertSoil(agrEquipment,agrEquipmentTr);
            return AjaxResult.success("新增设备成功");
        } catch (Exception e) {
            return AjaxResult.error("新增设备失败");
        }
    }

    private static AgrEquipmentTr getAgrEquipmentTr(SoilEquipment soilDetail) {
        AgrEquipmentTr agrEquipmentTr = new AgrEquipmentTr();
        agrEquipmentTr.setTrDiameter(soilDetail.getTrDiameter());
        agrEquipmentTr.setTrLength(soilDetail.getTrLength());
        agrEquipmentTr.setTrMaterial(soilDetail.getTrMaterial());
        agrEquipmentTr.setTrInterface(soilDetail.getTrInterface());
        agrEquipmentTr.setTrStorage(soilDetail.getTrStorage());
        agrEquipmentTr.setTrTime(soilDetail.getTrTime());
        agrEquipmentTr.setTrProtection(soilDetail.getTrProtection());
        return agrEquipmentTr;
    }

    @RequiresPermissions("data:soil:update")
    @PostMapping("/update")
    public AjaxResult updateAgrEquipmentTr(@RequestBody SoilEquipment soilEquipment) {
        return agrEquipmentTrService.updateAgrEquipmentTr(soilEquipment);
    }
    @RequiresPermissions("data:soil:delete")
    @PostMapping("/delete")
    public AjaxResult delAgrEquipmentTrByEquipmentId(Long equipmentId) {
        return agrEquipmentTrService.delAgrEquipmentTrByEquipmentId(equipmentId);
    }


    @RequiresPermissions("data:soil:insertTimeData")
    @PostMapping("/insertTimeData")
    public AjaxResult insertSoilTimeData(@RequestBody SoilTimeData soilTimeData) {

        return agrEquipmentTrService.insertSoilTimeData(soilTimeData);
    }



}
